package com.baidu.model;

import java.sql.*;

public class Test {
    public static void main(String[] args) {

        Student student = new Student("1", "李慧宇", "100");
        delete(student);
    }

    //添加
    public static int insert(Student student) {
        //获取数据库连接
        Connection conn = JDBCutils.getConnection();
        int i = 0;
        //写SQL语句
        String sql = "insert into Student(city_name,city_number) values(?,?)";
        //创建Statment并发送命令，Statment对象用于将SQL语句发送到数据库中，或理解为执行SQL语句
        //PreparedStatement用于执行带参或不带参的预编译SQL语句
        PreparedStatement p;
        try {
            p = conn.prepareStatement(sql);
            p.setString(1, "独孤求败");
            p.setString(2, "31");
            //执行SQL语句并获取对象
            i = p.executeUpdate();
            if (i == 1) {
                System.out.println("success");
            } else {
                System.out.println("fail");
            }
            //关闭数据库资源
            p.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //更新
//    public static int update(Student student) {
//        Connection conn = JDBCutils.getConnection();
//        int i = 0;
//        String sql = "update Student set city_name='"+ student.getName()+"'where id=4";
//        PreparedStatement p;
//        try {
//            p = (PreparedStatement) conn.prepareStatement(sql);
//            i = p.executeUpdate();
//            System.out.println("result:"+i);
//            p.close();
//            conn.close();
//        }catch (SQLException e){
//            e.printStackTrace();
//        }
//        return i;
//    }
//}
    //查询
//    public static Integer getAll(Student student) {
//        Connection conn = JDBCutils.getConnection();
//        String sql = "select * from Student";
//        PreparedStatement p;
//        try {
//            p = (PreparedStatement) conn.prepareStatement(sql);
//            ResultSet re = p.executeQuery();
//            int col = re.getMetaData().getColumnCount();
//            System.out.println("========");
//            while (re.next()) {
//                for (int i = 1; i < col; i++) {
//                    System.out.print(re.getString(i) + "\t");
//                    if ((i == 2) && (re.getString(i).length() < 8)) {
//                        System.out.print("\t");
//                    }
//                }
//                System.out.println("");
//            }
//            System.out.println("===========");
//        } catch (SQLException e) {
//            e.printStackTrace();
//
//        }
//        return null;
//    }

      //删除
    public  static int delete(Student name){
        Connection conn = JDBCutils.getConnection();
        int i = 0;
        String sql = "delete from Student where id = 1";
        PreparedStatement p;
        try {
            p = (PreparedStatement)conn.prepareStatement(sql);
            i = p.executeUpdate();
            System.out.println("result；"+i);
            p.close();
            conn.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return i;
    }
}
